<script setup lang="ts">
import { getProductListAPI, reqCancelSale, reqRemoveSku, reqSaleSku, reqSkuInfo } from '@/apis/sku';
import { records, SkuData, SkuInfoData } from '@/types/sku';
import { onMounted, ref } from 'vue';

const page = ref(1)
const limit = ref(10)
const total = ref(0)
//控制抽屉显示与隐藏的字段
let drawer = ref<boolean>(false)
let skuInfo = ref<any>({})
// 存储sku商品数据
const skuList = ref<records>([])
// 获取已有的SKU商品
const getProductList = async () => {
    const res = await getProductListAPI(page.value, limit.value)
    skuList.value = res.data.records
    total.value = res.data.total
}

// 初始化调用
onMounted(() => {
    getProductList()
})
// 页数改变
const changePage = (currentPge: number) => {
    page.value = currentPge
    getProductList()
}
//当前页码大小改变
const changePageSize = (pageSize: number) => {
    limit.value = pageSize
    getProductList()
    page.value = 1
}
//商品的上架与下架的操作
const updateSale = async (row: SkuData) => {
    //如果当前商品的isSale==1,说明当前商品是上架的额状态->更新为下架
    //否则else情况与上面情况相反
    if (row.isSale == 1) {
        //下架操作
        await reqCancelSale(row.id as number)
        //提示信息
        ElMessage({ type: 'success', message: '下架成功' })
        //发请求获取当前更新完毕的全部已有的SKU
        getProductList()
    } else {
        //下架操作
        await reqSaleSku(row.id as number)
        //提示信息
        ElMessage({ type: 'success', message: '上架成功' })
        //发请求获取当前更新完毕的全部已有的SKU
        getProductList()
    }
}
//更新已有的SKU
const updateSku = () => {
    ElMessage({ type: 'success', message: '程序员在努力的更新中....' })
}
//查看商品详情按钮的回调
const findSku = async (row: SkuData) => {
  //抽屉展示出来
  drawer.value = true
  //获取已有商品详情数据
  let result: SkuInfoData = await reqSkuInfo(row.id as number)
  //存储已有的SKU
  skuInfo.value = result.data
}
//删除某一个已有的商品
const removeSku = async (id: number) => {
    //删除某一个已有商品的情况
    let result: any = await reqRemoveSku(id)
    if (result.code == 200) {
        //提示信息
        ElMessage({ type: 'success', message: '删除成功' })
        //获取已有全部商品
        if (skuList.value.length > 1) {
            getProductList()
        } else {
            page.value - 1
            getProductList()
        }
    } else {
        //删除失败
        ElMessage({ type: 'error', message: '系统数据不能删除' })
    }
}
</script>

<template>
    <el-card>
        <!-- 表格组件 -->
        <el-table style="margin: 10px 0px;" border :data="skuList">
            <el-table-column label="序号" width="80px" type="index" align="center"></el-table-column>
            <el-table-column label="名称" prop="skuName" width="150px" show-overflow-tooltip></el-table-column>
            <el-table-column label="描述" prop="skuDesc" width="150px" show-overflow-tooltip></el-table-column>
            <el-table-column label="图片" width="150px">
                <template #="{ row }">
                    <img :src="row.skuDefaultImg" alt="" style="width: 100px; height: 100px;">
                </template>
            </el-table-column>
            <el-table-column label="重量" prop="weight" width="150px"></el-table-column>
            <el-table-column label="价格" prop="price" width="150px"></el-table-column>
            <el-table-column label="SPU操作" width="250px" fixed="right">
                <template #="{ row }">
                    <el-button type="primary" :icon="row.isSale == 1 ? 'Bottom' : 'Top'" size="small"
                        @click="updateSale(row)"></el-button>
                    <el-button type="primary" icon="Edit" size="small" @click="updateSku"></el-button>
                    <el-button type="primary" icon="InfoFilled" size="small" @click="findSku(row)"></el-button>
                    <el-popconfirm :title="`你确认删除${row.skuName}吗?`" @confirm="removeSku(row.id)">
                        <template #reference>
                            <el-button type="primary" icon="Delete" size="small"></el-button>
                        </template>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-pagination v-model:current-page="page" v-model:page-size="limit" :page-sizes="[3, 5, 8, 10]"
            :background="true" layout="prev, pager, next, jumper,->,sizes, total," :total="total"
            @current-change="changePage" @size-change="changePageSize" />
        <!-- 抽屉组件:展示商品详情 -->
        <el-drawer v-model="drawer">
            <!-- 标题部分 -->
            <template #header>
                <h4>查看商品的详情</h4>
            </template>
            <template #default>
                <el-row style="margin: 10px 0px">
                    <el-col :span="6">名称</el-col>
                    <el-col :span="18">{{ skuInfo.skuName }}</el-col>
                </el-row>
                <el-row style="margin: 10px 0px">
                    <el-col :span="6">描述</el-col>
                    <el-col :span="18">{{ skuInfo.skuDesc }}</el-col>
                </el-row>
                <el-row style="margin: 10px 0px">
                    <el-col :span="6">价格</el-col>
                    <el-col :span="18">{{ skuInfo.price }}</el-col>
                </el-row>
                <el-row style="margin: 10px 0px">
                    <el-col :span="6">平台属性</el-col>
                    <el-col :span="18">
                        <el-tag style="margin: 5px" v-for="item in skuInfo.skuAttrValueList" :key="item.id">
                            {{ item.valueName }}
                        </el-tag>
                    </el-col>
                </el-row>
                <el-row style="margin: 10px 0px">
                    <el-col :span="6">销售属性</el-col>
                    <el-col :span="18">
                        <el-tag style="margin: 5px" v-for="item in skuInfo.skuSaleAttrValueList" :key="item.id">
                            {{ item.saleAttrValueName }}
                        </el-tag>
                    </el-col>
                </el-row>
                <el-row style="margin: 10px 0px">
                    <el-col :span="6">商品图片</el-col>
                    <el-col :span="18">
                        <el-carousel :interval="4000" type="card" height="200px">
                            <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
                                <img :src="item.imgUrl" alt="" style="width: 100%; height: 100%" />
                            </el-carousel-item>
                        </el-carousel>
                    </el-col>
                </el-row>
            </template>
        </el-drawer>
    </el-card>
</template>

<style scoped>
.el-carousel__item h3 {
  color: #475669;
  opacity: 0.75;
  line-height: 200px;
  margin: 0;
  text-align: center;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>
